package com.szqhyxj.youxiangju.biz.merchantHome.shop.site;

import com.szqhyxj.youxiangju.biz.merchantHome.bean.DefaultSite;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

class MySitePresenter extends AbBasePresenter implements MySiteContract.Presenter {

    private final MySiteContract.View view;

    public MySitePresenter(MySiteContract.View view){
        this.view = view;
    }

    @Override
    public void init(){
        addDisposables(SiteModel.getDefaultquery()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
        .subscribe(new Consumer<BaseResult<List<DefaultSite>>>(){
            @Override
            public void accept(BaseResult<List<DefaultSite>> listBaseResult) throws Exception{
                view.dismissLoading();
                if(listBaseResult.businessLogicSuccess()){
                    if(listBaseResult.getData().size()>0){
                        view.showSiteList(listBaseResult.getData());
                    }else {
                        view.showNoSite();
                    }
                }else{
                    view.showToast(listBaseResult.getMessage());
                }
            }
        }, new Consumer<Throwable>(){
            @Override
            public void accept(Throwable throwable) throws Exception{

                view.showToast(throwable.getMessage());
                view.dismissLoading();
                view.showNoSite();
            }
        }));
    }

    @Override
    public void deleteDddress(String addressId){
        addDisposables(SiteModel.deleteDddress(addressId).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>(){
                    @Override
                    public void accept(BaseResult baseResult) throws Exception{
                        view.dismissLoading();
                        if(baseResult.businessLogicSuccess()){
                            view.showRefresh();
                        }else{
                            view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.dismissLoading();
                    }
                }));
    }

    @Override
    public void editDddress(DefaultSite defaultSite){
        view.navEditDddress(defaultSite);
    }

    @Override
    public void setDefaults(String addressId){
        addDisposables(SiteModel.setDefaults(addressId).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>(){
                    @Override
                    public void accept(BaseResult baseResult) throws Exception{
                        view.dismissLoading();
                        if(baseResult.businessLogicSuccess()){
                            view.showRefresh();
                        }else{
                            view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showToast(throwable.getMessage());
                        view.dismissLoading();
                    }
                }));
    }

    @Override
    public void addDisposables(Disposable disposable){
        view.showLoading();
        super.addDisposables(disposable);
    }
}